﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBLL;
using IDAL;
using Models.DTO;
using Models;

namespace BLL
{
	public class CustomerAddressBLL : ICustomerAddressBLL
    {
        private ICustomerAddressDAL _customerAddressDAL;
        private ICustomerInfoDAL _customerInfoDAL;

        public CustomerAddressBLL(
            ICustomerAddressDAL customerAddressDAL,
            ICustomerInfoDAL customerInfoDAL
            )
        {
            _customerAddressDAL = customerAddressDAL;
            _customerInfoDAL = customerInfoDAL;
        }

        #region 获取顾客地址表的方法 (GetCustomerAddresses)
        /// <summary>
        /// 获取顾客地址表的方法
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="address"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<GetCustomerAddressDTO> GetCustomerAddress(int limit, int page, string address, out int count)
        {
            var data = _customerAddressDAL.GetEntities()
                       .Where(r => r.Id != null)
                       .Select(r => new GetCustomerAddressDTO
                       {
                           Id = r.Id,
                           Address = r.Address,
                           Description = r.Description,
                           CreateTime = r.CreateTime,
                       });

            if (!string.IsNullOrWhiteSpace(address))
            {
                data = data.Where(d => d.Address.Contains(address));
            }
            //计算数据总数
            count = data.Count();
            //分页(降序)
            return data.OrderByDescending(it => it.CreateTime).Skip(limit * (page - 1)).Take(limit).ToList();
        }
        #endregion

        #region 创建顾客地址的方法 (CreateCustomerAddress)
        /// <summary>
        /// 创建顾客地址的方法
        /// </summary>
        /// <param name="cust">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateCustomerAddressH(CustomerAddress cust, out string msg)
        {
            if (string.IsNullOrWhiteSpace(cust.Address))
            {
                msg = "地址不能为空";
                return false;
            }
            //验证商品分类是否存在
            if (_customerAddressDAL.GetEntities().FirstOrDefault(it => it.Address == cust.Address) != null)
            {
                msg = "地址已经存在";
                return false;
            }
            cust.Id = Guid.NewGuid().ToString();//用户id


            cust.CreateTime = DateTime.Now;//设置时间
            bool IsSuccess = _customerAddressDAL.CreateEntity(cust);//调用方法
            msg = IsSuccess ? $"添加成功" : "添加失败";

            return IsSuccess;
        }
        #endregion

        #region 创建顾客地址的方法 (CreateCustomerAddress)
        /// <summary>
        /// 创建顾客地址的方法
        /// </summary>
        /// <param name="cust">用户实体</param>
        /// <param name="msg">返回消息</param>
        /// <returns></returns>
        public bool CreateCustomerAddress(CustomerAddress cust,string customerId, out string msg)
        {
            if (string.IsNullOrWhiteSpace(cust.Address))
            {
                msg = "地址不能为空";
                return false;
            }
            //验证商品分类是否存在
            if (_customerAddressDAL.GetEntities().FirstOrDefault(it => it.Address == cust.Address) != null)
            {
                msg = "地址没有更改";
                return false;
            }
            cust.Id = Guid.NewGuid().ToString();//用户id
            CustomerInfo customerInfo = _customerInfoDAL.GetEntityById(customerId);
            _customerAddressDAL.DeleteEntityById(customerInfo.AddressId);//调用方法
            
            customerInfo.AddressId = cust.Id;
            cust.CreateTime = DateTime.Now;//设置时间
            bool IsSuccess = _customerAddressDAL.CreateEntity(cust);//调用方法
            msg = IsSuccess ? $"添加成功" : "添加失败";

            return IsSuccess;
        }
        #endregion

        #region 删除顾客地址的方法 (DeleteCustomerAddress)
        /// <summary>
        /// 删除顾客地址的方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteCustomerAddress(string id)
        {
            CustomerAddress cust = _customerAddressDAL.GetEntities().FirstOrDefault(it=>it.Id==id);
            if (cust == null)
            {
                return false;
            }
            _customerAddressDAL.DeleteEntity(cust);
            return true;
        }
        #endregion

        #region 批量删除顾客地址的方法 (DeleteListCustomerAddress)
        /// <summary>
        /// 批量删除顾客地址的方法
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteListCustomerAddress(List<string> ids)
        {
            List<CustomerAddress> custList = _customerAddressDAL.GetEntities().Where(it => ids.Contains(it.Id)).ToList();
            foreach (var item in ids)
            {
                CustomerAddress cust = _customerAddressDAL.GetEntities().FirstOrDefault(it=>it.Id==item);
                if (custList == null)
                {
                    return false;
                }
                _customerAddressDAL.DeleteEntity(cust);
            }
            return true;
        }
        #endregion

        #region 修改顾客地址的方法 (UpdateCustomerAddress)
        /// <summary>
        /// 修改顾客地址的方法
        /// </summary>
        /// <param name="cust"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool UpdateCustomerAddress(CustomerAddress cust, out string msg)
        {
            if (string.IsNullOrWhiteSpace(cust.Address))
            {
                msg = $"地址不能为空";
                return false;
            }
            CustomerAddress entity = _customerAddressDAL.GetEntities().FirstOrDefault(it=>it.Id== cust.Id);
            if (entity.Id == null)
            {
                msg = "ID不存在";
                return false;
            }
            //判断重复
            if (entity.Address != cust.Address)
            {
                var data = _customerAddressDAL.GetEntities().FirstOrDefault(it => it.Address == cust.Address);
                if (data != null)
                {
                    msg = "商品分类名已经被占用";
                    return false;
                }
            }

            entity.Address = cust.Address;
            entity.Description = cust.Description;

            bool IsSuccess = _customerAddressDAL.UpdateEntity(entity);//调用方法

            msg = IsSuccess ? $"修改成功" : "修改失败";

            return IsSuccess;
        }
        #endregion
    }
}
